package org.visionlibrary.image.filters.effects;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.acquisition.ImageFactory;
import org.visionlibrary.image.model.AbstractFilter;


public class CutCircle extends AbstractFilter {
	@Override
	public TiledImage applyFilter(TiledImage src, TiledImage dest) {
		if (null == src)
			throw new NullPointerException("Source image is null.");

		if (null == dest)
			dest = (new ImageFactory()).createCompatibleImage(src);
		
		int maxX = src.getWidth();
		int maxY = src.getHeight();

		for (int x = 0; x < maxX; x++)
			for (int y = 0; y < maxY; y++) {
				if (((((double) x - (double) maxX / 2.0) * ((double) x - (double) maxX / 2.0)) / (((double) maxX / 2.0) * ((double) maxX / 2.0)))
						+ ((((double) y - (double) maxY / 2.0) * ((double) y - (double) maxY / 2.0)) / (((double) maxY / 2.0) * ((double) maxY / 2.0))) < 1.0) {

					for (int ch = 0; ch < src.getNumBands(); ch++) {
						int newPixelVal = src.getSample(x, y, ch);
						dest.setSample(x, y, ch, newPixelVal);
					}
				} else {
					for (int ch = 0; ch < src.getNumBands(); ch++) {
						dest.setSample(x, y, ch, 0);
					}
				}

			}

		return dest;
	}
}
